package com.yuda.campsite.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdcardUtil;
import com.yuda.common.core.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yuda.common.log.annotation.Log;
import com.yuda.common.log.enums.BusinessType;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.campsite.domain.StudentInfo;
import com.yuda.campsite.service.*;
import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author wyn
 * @date 2021-09-27
 */
@RestController
@RequestMapping("/info")
public class StudentInfoController extends BaseController
{
    @Autowired
    private IStudentInfoService studentInfoService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize(hasPermi = "common:info:list")
    @GetMapping("/list")
    public TableDataInfo list(StudentInfo studentInfo)
    {
        startPage();
        List<StudentInfo> list = studentInfoService.selectStudentInfoList(studentInfo);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize(hasPermi = "common:info:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentInfo studentInfo) throws IOException
    {
        List<StudentInfo> list = studentInfoService.selectStudentInfoList(studentInfo);
        ExcelUtil<StudentInfo> util = new ExcelUtil<StudentInfo>(StudentInfo.class);
        util.exportExcel(response, list, "info");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize(hasPermi = "common:info:query")
    @GetMapping(value = "/{uuid}")
    public AjaxResult getInfo(@PathVariable("uuid") String uuid)
    {
        return AjaxResult.success(studentInfoService.selectStudentInfoById(uuid));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize(hasPermi = "common:info:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentInfo studentInfo)
    {
        return toAjax(studentInfoService.insertStudentInfo(studentInfo));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize(hasPermi = "common:info:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    /**学生的uuid是唯一不可变的
     * 再更新之前必须先将UUID查询出来，然后改变了身份证号码之后，需要根据UUID更新所有这个UUID有关的表中数据*/
    public AjaxResult edit(@RequestBody StudentInfo studentInfo)
    {
        /**首先判断必填信息不能为空,利人利己*/
        if (studentInfo.getEmail()==null||studentInfo.getIdCard()==null||studentInfo.getPhone() ==null) {
            return AjaxResult.error("email或者身份证号或者电话号码不能修改为空");
        }
        /**验证身份证号是否正确是否正确*/
        boolean validCard = IdcardUtil.isValidCard(studentInfo.getIdCard());
        if (validCard) {
            studentInfoService.OrderIdCard(studentInfo.getIdCard(), studentInfo.getUuid());
            studentInfoService.OrderInfoIdCard(studentInfo.getIdCard(), studentInfo.getUuid());
            studentInfoService.StudentAccount(studentInfo.getIdCard(), studentInfo.getUuid());
            studentInfoService.StudentAccountEmail(studentInfo.getEmail(), studentInfo.getUuid());
            studentInfoService.StudentAccountPhone(studentInfo.getPhone(), studentInfo.getUuid());
            studentInfoService.updateStudentInfo(studentInfo);
            return AjaxResult.success("修改成功啦");
        }else {
            return AjaxResult.error("身份证号码不正确");
        }
        /*
        return toAjax(studentInfoService.updateStudentInfo(studentInfo));
*/
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize(hasPermi = "common:info:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uuids}")
    public AjaxResult remove(@PathVariable String[] uuids)
    {
        return toAjax(studentInfoService.deleteStudentInfoByIds(uuids));
    }


}
